package com.hwd.srb.core.controller.api;


import com.hwd.srb.common.constant.BorrowConstants;
import com.hwd.srb.common.constant.Constants;
import com.hwd.srb.common.result.R;
import com.hwd.srb.common.util.JwtUtils;
import com.hwd.srb.core.enmu.BorrowAuthEnum;
import com.hwd.srb.core.enmu.BorrowerStatusEnum;
import com.hwd.srb.core.pojo.vo.BorrowerVO;
import com.hwd.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author 黄伟东/Victor
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/api/core/borrower")
@Api(tags = "借款人接口")
public class ApiBorrowerController {

    @Resource
    private BorrowerService borrowerService;

    /**
     * 保存借款人信息
     * @param borrowerVO 借款人信息对象
     * @param request http请求对象，获取token
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation("保存借款人信息")
    @PostMapping("/save")
    public R save(@ApiParam(value = "借款人对象", required = true) @RequestBody BorrowerVO borrowerVO,
                  HttpServletRequest request) {
        String token = request.getHeader(Constants.TOKEN);
        Long userId = JwtUtils.getUserId(token);
        borrowerService.saveBorrowerVOByUserId(borrowerVO,userId);

        return R.ok().message(Constants.SUBMIT_APPLICATIONS_SUCCESS);
    }

    /**
     * 根据userId获取借款人额度申请状态
     *
     * @param request http请求对象，获取token
     * @return com.hwd.srb.common.result.R
     */
    @ApiOperation("取借款人额度申请状态")
    @GetMapping("/getBorrowerStatusByUserId")
    public R getBorrowerStatusByUserId(HttpServletRequest request) {
        // 从token中获取userId
//        String token = request.getHeader(UserInfoConstants.TOKEN);
//        boolean bToken = JwtUtils.checkToken(token);
//        Assert.isTrue(bToken, ResponseEnum.WEIXIN_FETCH_USERINFO_ERROR);
//        Long userId = JwtUtils.getUserId(token);
        Long userId = Long.valueOf(request.getHeader(Constants.USER_ID));

        BorrowerStatusEnum status = borrowerService.getBorrowerStatusByUserId(userId);

        return R.ok().data(BorrowConstants.BORROWER_STATUS, status);

    }

}

